// formPackage/pages/newHousePublishModule/newHousePublishSecond/newHousePublishSecond.js
import { createNewHouseImgList, queryNewHouseImgList } from "../../../../service/publish_service"
import { chooseMultipleImgUploadQiniuAction, chooseAndUploadQiniuVideoAction } from "../../../../service/upload_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    imgArr: [

    ],
    houseNumber: "",
    formData: {
      effectPics: [],//效果图
      projectAround: [],//项目周边
      realPics: [],//实景图
      samplePics: [],//样板间
      video: null,//视频
      zonePic: [],//区位图
    },
    btnTitle: "保存",
    isHaveImgList: false,//是否已有图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.houseNumber) {
      this.setData({
        houseNumber: options.houseNumber
      })
      this.queryNewHouseImgList();
    }

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //查询已有图片:
  queryNewHouseImgList() {
    wx.showLoading();
    queryNewHouseImgList(this.data.houseNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:新房已有图片:" + JSON.stringify(res));
      if (res.code == 200) {
        let imgData = res.data;
        if (imgData.effectPics || imgData.samplePics || imgData.realPics ||
          imgData.zonePic || imgData.projectAround || imgData.video
        ) {
          this.setData({
            formData: res.data,
            btnTitle: "更新",
            isHaveImgList: true
          })
        } else {
          this.setData({
            formData: res.data,
          })
        }

      }
    }).catch(err => {
      wx.hideLoading();
      console.log("JJLog:新房图片失败:" + JSON.stringify(err));
    })
  },



  /**图片控件回调 */

  //上传图片控件上传点击:
  pushImgAction(e) {
    console.log("JJLog:上传图片方法点击来了:" + JSON.stringify(e));
    let type = e.detail.type;
    this.pushMultiImgs(type);
  },


  //图片控件删除点击
  deleteImgAction(e) {
    // console.log("JJLog:图片控件删除点击:" + JSON.stringify(e));
    let type = e.detail.type;
    let index = e.detail.deleteIndex;
    let arr = this.setTempImgArrByType(type);
    arr.splice(index, 1);
    this.setFormDataImg(type, arr);

  },
  //图片预览回调:
  previewImgAction(e) {
    console.log("JJLog:图片预览回调:" + JSON.stringify(e));
  },



  /**图片控件回调 */
  //图片临时数组赋值:
  setTempImgArrByType(type) {
    let arr = [];
    switch (type) {
      case "effect":
        arr = this.data.formData.effectPics || [];
        break;
      case "real":
        arr = this.data.formData.realPics || [];
        break;
      case "around":
        arr = this.data.formData.projectAround || [];
        break;
      case "sample":
        arr = this.data.formData.samplePics || [];
        break;
      case "zone":
        arr = this.data.formData.zonePic || [];
        break;

      default:
        break;
    }
    return arr;
  },

  // 图片上传:
  //上传图片:
  /**
   * 
   *  COVER_PIC(1, "封面图","cover_pic"),
    EFFECT_PIC(2, "效果图","effect_pic"),
    SAMPLE_PIC(3, "样板间","sample_pic"),
    REAL_PIC(4, "实际图","real_pic"),
    ZONE_PIC(9, "区位图","zone_pic"),
    PROJECT_AROUND(10, "项目周边","project_around"),
    HOUSE_VIDEO(11, "项目视频","project_video"),
    ERROR(9999, "失败图","error"),
   */
  pushMultiImgs(type) {
    let arr = [];
    let imgType = 2;
    switch (type) {
      case "effect":
        arr = this.data.formData.effectPics || [];
        imgType = 2;
        break;
      case "real":
        arr = this.data.formData.realPics || [];
        imgType = 4;
        break;
      case "around":
        arr = this.data.formData.projectAround || [];
        imgType = 10;
        break;
      case "sample":
        arr = this.data.formData.samplePics || [];
        imgType = 3;
        break;
      case "zone":
        arr = this.data.formData.zonePic || [];
        imgType = 9;
        break;

      default:
        break;
    }

    chooseMultipleImgUploadQiniuAction('newImg').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })


      res.forEach((item) => {
        let obj = {
          "fileType": 1,
          "height": item.height,
          "type": imgType,
          "url": item.complateUrl,
          "width": item.width
        }
        arr.push(obj);
      })
      this.setFormDataImg(type, arr);
      // console.log("JJLog:arr=" + JSON.stringify(arr));
    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },
  //图片赋值
  setFormDataImg(type, arr) {
    switch (type) {
      case "effect":
        this.setData({
          'formData.effectPics': arr
        })
        break;
      case "real":
        this.setData({
          'formData.realPics': arr
        })
        break;
      case "around":
        this.setData({
          'formData.projectAround': arr
        })
        break;
      case "sample":
        this.setData({
          'formData.samplePics': arr
        })
        break;
      case "zone":
        this.setData({
          'formData.zonePic': arr
        })
        break;

      default:
        break;
    }
  },
  //上传图片数据:
  pushBasicInfo() {
    this.setData({
      'formData.houseNumber': this.data.houseNumber
    })
    debounce(this.createNewHouseImgList);
  },
  //提交图片数据
  createNewHouseImgList() {
    wx.showLoading({
      title: '保存中...',
    })
    createNewHouseImgList(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '保存成功',
          icon: "success"
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
      })
    })
  },
  //上传户型视频
  pushFormatVideo() {
    let arr = [];
    chooseAndUploadQiniuVideoAction('format').then(imageRes => {
      console.log("JJLog:上传视频成功后返回===:" + JSON.stringify(imageRes));
      // this.setData({
      //   'formData.videoUrl': imageRes.complateUrl,
      // })
      let obj = {
        "fileType": 2,
        "type": 11,
        "url": imageRes.complateUrl,
      }
      arr.push(obj);
      this.setData({
        'formData.video': arr
      })
      wx.showToast({
        title: '上传成功',
        icon: "none"
      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '上传失败',
        icon: "none"
      })
    })
  },
  cancelVideoAction() {
    this.setData({
      'formData.video': null
    })
  },

  //到下一步
  pushNextAction() {
    wx.navigateTo({
      url: '/formPackage/pages/newHousePublishModule/housePublishFormatList/housePublishFormatList?houseNumber=' + this.data.houseNumber,
    })
  },




})